package org.rmc.demo.util;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Http工具类
 */
public class HttpUtil {

    private static RequestConfig config;

    static{
        config = RequestConfig.custom()
                .setSocketTimeout(5000)
                .setConnectTimeout(5000)
                .setConnectionRequestTimeout(5000)
                .build();
    }

    private static HttpUtil instance = null;

    public static HttpUtil getInstance(){
        if (instance == null) {
            instance = new HttpUtil();
        }
        return instance;
    }

    public String post(String url, Map<String, String> params) {
        String result = null;
        try {
            List<NameValuePair> valuePairList = new ArrayList<>();
            Set<String> keySet = params.keySet();
            for (String key : keySet){
                String value = params.get(key);
                NameValuePair valuePair = new BasicNameValuePair(key, value);
                valuePairList.add(valuePair);
            }
            CloseableHttpClient client = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setConfig(config);
            httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
            UrlEncodedFormEntity se = new UrlEncodedFormEntity(valuePairList, "utf-8");
            httpPost.setEntity(se);
            CloseableHttpResponse response = client.execute(httpPost);
            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity, "UTF-8");
            EntityUtils.consume(entity);
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    public String postToJson(String httpUrl, String paramJson) {
        String result = null;
        try {
            CloseableHttpClient client = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(httpUrl);
            httpPost.setConfig(config);
            httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
            StringEntity se = new StringEntity(paramJson);
            se.setContentType("text/json");
            httpPost.setEntity(se);
            CloseableHttpResponse response = client.execute(httpPost);
            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity, "UTF-8");
        } catch (IOException e){
            e.printStackTrace();
        }
        return result;
    }

}
